package com.techstar.api.service.bus;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.net.URLEncoder;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.techstar.api.pojo.bus.TodayTimeExecutionRank;
import com.techstar.api.repos.bus.TodayTimeExecutionRankRepos;
import com.techstar.api.service.ConfigProperties;
import com.techstar.api.service.GateWayApiProperties;
import com.techstar.api.service.HttpClientHeper;
import com.techstar.api.service.TrafficApiProperties;
import com.techstar.common.jackson.JacksonMapper;
import com.techstar.common.pojo.InterfaceType;
import com.techstar.common.utils.InterfaceUtils;
import com.techstar.roma.http.GatewayService;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author EinIce
 * @description 当日正点执行率排名
 * @date 2021年7月29日
 */

@Slf4j
@Service
public class TodayTimeExecutionRankService {
	
	@Autowired
	private TodayTimeExecutionRankRepos beanRepos;
	
	private static final ObjectMapper objectMapper = JacksonMapper.instance.objectMapper;
	
	@Autowired
	private ConfigProperties configProperties; // 配置文件注入
	
	public void query(){
		GateWayApiProperties gateWayApiProperties = configProperties.busRomaApiProperties();
		TrafficApiProperties trafficApiProperties = configProperties.tterApiProperties();
		if(gateWayApiProperties.testMode==true&&!"test".equals(trafficApiProperties.able)
				||"disable".equals(trafficApiProperties.able)) {
			log.info("DistrictCongestTrendService out");
			return;
		}
		try {
			String currentTime = URLEncoder.encode(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"), "UTF-8").replaceAll("\\+", "%20");
			trafficApiProperties.param = String.format("current_time=%s", currentTime);
			HttpGet httpGet = new HttpGet();
			httpGet = (HttpGet) GatewayService.GateWay(httpGet,gateWayApiProperties, trafficApiProperties);
			CloseableHttpResponse response = null;
			String result = null;
            response = HttpClientHeper.instance.httpclient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
            log.info(result);
            JsonNode jsonNode = objectMapper.readTree(result.toString()).get("data");
            try {
            	InterfaceType.TODAY_TIME_EXECUTION_RANK.setApiUrl(httpGet.toString());
            	InterfaceType.TODAY_TIME_EXECUTION_RANK.setClassName(this.getClass().getSimpleName());
            	InterfaceUtils.filtrateDate(InterfaceType.TODAY_TIME_EXECUTION_RANK, DateTime.now(), jsonNode);
            } catch (Exception e) {
            	log.error("接口记录失败-"+this.getClass().getSimpleName()+":"+e.getMessage());
            }
            jsonNode = jsonNode.get("max");
            if(jsonNode==null)return;
			List<TodayTimeExecutionRank> list = objectMapper.readValue(jsonNode.toString(),new TypeReference<List<TodayTimeExecutionRank>>() {});
			beanRepos.saveAll(list);
			jsonNode = objectMapper.readTree(result.toString()).get("data").get("min");
			List<TodayTimeExecutionRank> list2 = objectMapper.readValue(jsonNode.toString(),new TypeReference<List<TodayTimeExecutionRank>>() {});
			beanRepos.saveAll(list2);
            response.close();
        }catch (Exception e) {
        	e.printStackTrace();
        	log.error(e.getMessage());
		}
	}
	public static void main(String[] args) throws IOException{
		Reader reader = new FileReader("D:\\工作\\恒泰\\交委\\数据样例\\当日正点执行率排名.json");
		int ch = reader.read();
		StringBuffer buffer = new StringBuffer();
		while (ch != -1) { // 读取成功
			buffer.append((char) ch);
			ch = reader.read();
		}
//		System.out.println(buffer.toString());
		// 3、关闭流
		reader.close();
		String result = buffer.toString();
		JsonNode jsonNode = JacksonMapper.instance.objectMapper.readTree(result).get("data").get("max");
		List<TodayTimeExecutionRank> readValue= objectMapper.readValue(jsonNode.toString(), new TypeReference<List<TodayTimeExecutionRank>>() {});
//		readValue.forEach(p->System.out.println(Integer.valueOf((int) p.getCreateTime().getMillis())));
//		readValue.forEach(p->System.out.println(p.getCreateTime().getMillis()));
		System.out.println(readValue.toString());
	}
}
